package MTT::Libs::ReportData;

use strict;

use base 'Class::Singleton';
use MTT;
use MTT::Schema;

use Data::Dumper;

sub _new_instance {
	my $class = shift;

	my $ci = MTT->config->{'Model::DBIC'}->{connect_info};
	my $schema = MTT::Schema->connect($ci);

	my $self = { schema => $schema };
	bless $self, $class;
	$self->reload();

	return $self;
}

sub get_data {
	my ($self) = @_;

	return $self->{data};
}

sub reload {
	my ($self, $schema) = @_;

	$schema ||= $self->{schema};
	die "schema need for reportStore" unless $schema;

	my $companies = $schema->resultset('Trunk')->search(
		{}, 
		{columns => [ qw(company_name) ], distinct => 1, order_by => ['company_name']}
	);
	my @companies;
	while ( my $comp = $companies->next() ) {
		push @companies, $comp;
	}

	my $cities = $schema->resultset('Trunk')->search(
		{}, 
		{columns => [ qw(city) ], distinct => 1, order_by => ['city']}
	);
	my @cities;
	while ( my $city = $cities->next() ) {
		push @cities, $city;
	}

	my $obls = $schema->resultset('Trunk')->search(
		{}, 
		{columns => [ qw(oblast) ], distinct => 1, order_by => ['oblast']}
	);
	my @obls;
	while ( my $obl = $obls->next() ) {
		push @obls, $obl;
	}


	my $fos = $schema->resultset('Trunk')->search(
		{}, 
		{columns => [ qw(fo) ], distinct => 1, order_by => ['fo']}
	);
	my @fos;
	while ( my $fo = $fos->next() ) {
		push @fos, $fo;
	}

	my @users = $schema->resultset('User')->all();
	my @switch_types = $schema->resultset('SwitchType')->all();

	
	$self->{data} = {
		companies => \@companies,
		cities => \@cities,
		obls => \@obls,
		fos	 => \@fos,
		users => \@users,
		switch_types => \@switch_types
	};
	
}

1;
